package cn.tedu.sp04.controller;

import cn.tedu.sp01.pojo.Item;
import cn.tedu.sp01.pojo.Order;
import cn.tedu.sp01.pojo.User;
import cn.tedu.sp01.service.OrderService;
import cn.tedu.sp01.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;

/**
 * @author 张琪
 * @date 2020/12/18
 * @describe
 */
@RestController
public class OrderController {


    @Autowired
    private OrderService orderService;

    @GetMapping("/{orderId}")
    public JsonResult<Order> getOrder(@PathVariable String orderId) {
        Order order = orderService.getOrder(orderId);
        return JsonResult.ok().data(order);
    }

    @GetMapping("/")
    public JsonResult<?> addOrder(){
        Order order = new Order();
        order.setId("111111111");
        order.setUser(new User(8, null, null));
        order.setItems(Arrays.asList(
            new Item(1,"aaa",2),
            new Item(2,"bbb",1),
            new Item(3,"ccc",3),
            new Item(4,"ddd",1),
            new Item(5,"eee",5)
        ));

        orderService.addOrder(order);
        return JsonResult.ok().msg("保存订单成功");
    }

}
